﻿/***********************************************************************************************
 * 
 * ProjectName: WMSSoft.WinKStart
 * 
 * Description: Provide the ViewModel used for Settings_NewEditDSCustomItem Dialog
 * 
 * Created:     06.09.2015
 * 
 * Author:      matthias wagler - matthias@WMSSoft.de
 * 
 * Copyright:   Copyright 2015 by WMSSoft.de
 * 
 * License:     GPLv3
 * 
 * History:     06.09.2015 - matthias wagler matthias@WMSSoft.de   -   Created
 * 
 **********************************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Imaging;
using WMSSoft.Lib.Basic;
using WMSSoft.Lib.Texte;
using WMSSoft.Lib.Enums;
using WMSSoft.Lib.WinKStart;
using WMSSoft.Lib.WinKStart.Classes.Metadata;
using WMSSoft.Lib.WinKStart.Abstract;
using WMSSoft.Lib.WinKStart.Model;
using WMSSoft.Lib.WinKStart.ViewModel;

namespace WMSSoft.WinKStart.ViewModel
{
    public class Settings_NewEditDSViewModel : SettingBaseListViewModel
    {
        List<KeyValueAssignment> _ItemTypeItems = null;
        List<KeyValueAssignment> _ExecutionTypeItems = null;
        List<KeyValueAssignment> _ExecutionProcedureItems = null;
        List<ImageKeyValueAssignment> _AvailableImages = null;
        cStartmenuItem _Data = null;
        ImageKeyValueAssignment _SelectedImage = null;
        bool _DataChanged = false;
        bool _CanEditName = true;
        bool _CanEditCommand = false;
        bool _CanEditProcedure = false;
        string _ItemName = "";
        string _ItemCaption = "";
        string _SelectedItemType = "";
        string _SelectedExecutionType = "";
        string _SelectedExecutionProcedure = "";
        string _ItemCommand = "";
        string _ItemCommandArguments = "";
        string _ItemWorkingDirectory = "";
        string _PanelID = "";
        string _Filter = "";        
        string _Comment = "";

        #region Initialize
        public Settings_NewEditDSViewModel()
        {
            try
            {
                base.ViewName = LanguageStrings.GetText("New Custom Item");

                InitItemTypeItems();
                InitExecutionTypeItems();
                InitExecutionProcedureItems();
                InitImageItems();

                PropertyChanged += Settings_NewEditDSViewModel_PropertyChanged;
                ViewModel_OkClick += Settings_NewEditDSViewModel_ViewModel_OkClick;
                ViewModel_CloseClick += Settings_NewEditDSViewModel_ViewModel_CloseClick;
                m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("..."), new CDelegatingCommand(Delegate => this.Cmd_Exec_ChooseFilter())));

                _Data = new cStartmenuItem();
            }
            catch (Exception Ex)
            {

            }
        }
      
        public Settings_NewEditDSViewModel(cStartmenuItem CustomItem)
        {
            try
            {
                base.ViewName = LanguageStrings.GetText("Edit Custom Item");

                InitItemTypeItems();
                InitExecutionTypeItems();
                InitExecutionProcedureItems();
                InitImageItems();

                PropertyChanged += Settings_NewEditDSViewModel_PropertyChanged;
                ViewModel_OkClick += Settings_NewEditDSViewModel_ViewModel_OkClick;
                ViewModel_CloseClick += Settings_NewEditDSViewModel_ViewModel_CloseClick;
                m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("..."), new CDelegatingCommand(Delegate => this.Cmd_Exec_ChooseFilter())));

                _Data = CustomItem;

                ItemName = Data.ItemName;
                ItemCaption = Data.ViewName;
                SelectedItemType = Data.ItemType.ToString();
                SelectedExecutionType = Data.ExecutionType.ToString();
                SelectedExecutionProcedure = Data.ExecutingProcedure.ToString();
                ItemCommand = Data.LinkFilename;
                ItemCommandArguments = Data.Arguments;
                ItemWorkingDirectory = Data.WorkingDirectory;
                PanelID = Data.GroupID;
                if (Data.Image != null)
                    SelectedImage = FindImageKey(Data.Image.UriSource.LocalPath);
                Filter = Data.Filter;
                //Comment = Data.
            }
            catch (Exception Ex)
            {                
                
                //throw;
            }
        }

        #endregion

        #region Properties

        public cStartmenuItem Data
        {
            get { return _Data; }
            set
            {
                _Data = value;
                OnPropertyChanged();
            }
        }

        public string ItemName
        {
            get { return _ItemName; }
            set
            {
                _ItemName = value;
                OnPropertyChanged();
            }
        }

        public bool CanEditName
        {
            get { return _CanEditName; }
            set
            {
                _CanEditName = value;
                OnPropertyChanged();
            }
        }

        public string ItemCaption
        {
            get { return _ItemCaption; }
            set
            {
                _ItemCaption = value;
                OnPropertyChanged();
            }
        }
        public List<KeyValueAssignment> ItemTypeItems
        {
            get { return _ItemTypeItems; }
            set
            {
                _ItemTypeItems = value;
                OnPropertyChanged();
            }
        }

        public string SelectedItemType
        {
            get { return _SelectedItemType; }
            set
            {
                _SelectedItemType = value;
                OnPropertyChanged();
            }
        }

        public List<KeyValueAssignment> ExecutionTypeItems
        {
            get { return _ExecutionTypeItems; }
            set 
            {
                _ExecutionTypeItems = value;
                OnPropertyChanged();
            }
        }

        public string SelectedExecutionType
        {
            get { return _SelectedExecutionType; }
            set
            {
                _SelectedExecutionType = value;
                OnPropertyChanged();
            }
        }

        public List<KeyValueAssignment> ExecutionProcedureItems
        {
            get { return _ExecutionProcedureItems; }
            set
            {
                _ExecutionProcedureItems = value;
                OnPropertyChanged();
            }
        }

        public string SelectedExecutionProcedure
        {
            get { return _SelectedExecutionProcedure; }
            set
            {
                _SelectedExecutionProcedure = value;
                OnPropertyChanged();
            }
        }

        public bool CanEditCommand
        {
            get { return _CanEditCommand; }
            set
            {
                _CanEditCommand = value;
                OnPropertyChanged();
            }
        }

        public bool CanEditProcedure
        {
            get { return _CanEditProcedure; }
            set
            {
                _CanEditProcedure = value;
                OnPropertyChanged();
            }
        }

        public string ItemCommand
        {
            get { return _ItemCommand; }
            set
            {
                _ItemCommand = value;
                OnPropertyChanged();
            }
        }

        public string ItemCommandArguments
        {
            get { return _ItemCommandArguments; }
            set
            {
                _ItemCommandArguments = value;
                OnPropertyChanged();
            }
        }

        public string ItemWorkingDirectory
        {
            get { return _ItemWorkingDirectory; }
            set
            {
                _ItemWorkingDirectory = value;
                OnPropertyChanged();
            }
        }

        public string PanelID
        {
            get { return _PanelID; }
            set
            {
                _PanelID = value;
                OnPropertyChanged();
            }
        }

        public string Filter
        {
            get { return _Filter; }
            set
            {
                _Filter = value;
                OnPropertyChanged();
            }
        }

        public ImageKeyValueAssignment SelectedImage
        {
            get { return _SelectedImage; }
            set
            {
                _SelectedImage = value;
                OnPropertyChanged();
            }
        }

        public string Comment
        {
            get { return _Comment; }
            set
            {
                _Comment = value;
                OnPropertyChanged();
            }
        }

        public List<ImageKeyValueAssignment> AvailableCustomImages
        {
            get { return _AvailableImages; }
            set
            {
                _AvailableImages = value;
                OnPropertyChanged();
            }
        }

        #endregion

        #region Command Procedures
        void Settings_NewEditDSViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            try
            {
                
                if (e.PropertyName == "SelectedExecutionType")
                {
                    if (SelectedExecutionType == "Code")
                    {
                        CanEditCommand = false;
                        CanEditProcedure = true;
                    }
                    else if (SelectedExecutionType == "Program")
                    {
                        CanEditCommand = true;
                        CanEditProcedure = false;
                    }
                    else
                    {
                        CanEditCommand = false;
                        CanEditProcedure = false;
                    }
                }
            }
            catch (Exception Ex)
            {

                throw new Exception("Error while changing Property!", Ex); ;
            }
        }

        void Settings_NewEditDSViewModel_ViewModel_OkClick(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

        void Settings_NewEditDSViewModel_ViewModel_CloseClick(object sender, EventArgs e)
        {
            try
            {
                Settings_NewEditDSCustomItem parentDlg = null;
                if ((Settings_NewEditDSCustomItem)((CDelegatingCommand)Cmd_CloseClick.Command).CommandParameter != null)
                {
                    parentDlg = (Settings_NewEditDSCustomItem)((CDelegatingCommand)Cmd_CloseClick.Command).CommandParameter;

                    if (_DataChanged == true)
                    {
                        if (System.Windows.MessageBox.Show(QuestionMessages.QUESTION_CLOSE_DIALOG__LOSE_CHANGES, "Question",
                            System.Windows.MessageBoxButton.YesNo) == System.Windows.MessageBoxResult.No)
                            return;
                    }

                    if (parentDlg != null)
                        parentDlg.Close();

                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage(LanguageStrings.GetText("Can't Close Dialog!"), Ex, EMessageTypes.Error);
            } 
        }

        void Cmd_Exec_ChooseFilter()
        {

        }

        #endregion

        #region Helpers

        void InitItemTypeItems()
        {
            try
            {
                List<KeyValueAssignment> tmpItemTypeItems = new List<KeyValueAssignment>();
                tmpItemTypeItems.Add(new KeyValueAssignment("", LanguageStrings.GetText("Please choose a Type")));
                tmpItemTypeItems.Add(new KeyValueAssignment(eItemType.Filter.ToString(), LanguageStrings.GetText(EnumUtils.GetEnumValueDesc(eItemType.Filter, "Filter"))));
                tmpItemTypeItems.Add(new KeyValueAssignment(eItemType.Folder.ToString(), LanguageStrings.GetText(EnumUtils.GetEnumValueDesc(eItemType.Folder, "Folder"))));
                tmpItemTypeItems.Add(new KeyValueAssignment(eItemType.Label.ToString(), LanguageStrings.GetText(EnumUtils.GetEnumValueDesc(eItemType.Label, "Label"))));
                tmpItemTypeItems.Add(new KeyValueAssignment(eItemType.Link.ToString(), LanguageStrings.GetText(EnumUtils.GetEnumValueDesc(eItemType.Link, "Link"))));
                //tmpItemTypeItems.Add(new KeyValueAssignment(eItemType.Root.ToString(), LanguageStrings.GetText(EnumUtils.GetEnumValueDesc(eItemType.Root, "Root"))));
                tmpItemTypeItems.Add(new KeyValueAssignment(eItemType.URL.ToString(), LanguageStrings.GetText(EnumUtils.GetEnumValueDesc(eItemType.URL, "URL"))));

                ItemTypeItems = tmpItemTypeItems;
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Unable to Init Action Types!", Ex, EMessageTypes.Error);
            }
        }
        void InitExecutionTypeItems()
        {
            try
            {
                List<KeyValueAssignment> tmpExecutionTypeItems = new List<KeyValueAssignment>();
                tmpExecutionTypeItems.Add(new KeyValueAssignment("", LanguageStrings.GetText("Please choose a Type")));
                tmpExecutionTypeItems.Add(new KeyValueAssignment(eLinkExecutionTypes.Code.ToString(), LanguageStrings.GetText(EnumUtils.GetEnumValueDesc(eLinkExecutionTypes.Code, "Code"))));
                tmpExecutionTypeItems.Add(new KeyValueAssignment(eLinkExecutionTypes.Program.ToString(), LanguageStrings.GetText(EnumUtils.GetEnumValueDesc(eLinkExecutionTypes.Program, "Program"))));
                
                ExecutionTypeItems = tmpExecutionTypeItems;
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Unable to Init Action Types!", Ex, EMessageTypes.Error);
            }
        }


        void InitExecutionProcedureItems()
        {
            try
            {
                List<KeyValueAssignment> tmpExecutionProceduresItems = new List<KeyValueAssignment>();
                tmpExecutionProceduresItems.Add(new KeyValueAssignment("", LanguageStrings.GetText("Please choose a Type")));
                tmpExecutionProceduresItems.Add(new KeyValueAssignment(eExecutionProcedure.energy_save_mode.ToString(), LanguageStrings.GetText(EnumUtils.GetEnumValueDesc(eExecutionProcedure.energy_save_mode, "energy_save_mode"))));
                tmpExecutionProceduresItems.Add(new KeyValueAssignment(eExecutionProcedure.hibernate_mode.ToString(), LanguageStrings.GetText(EnumUtils.GetEnumValueDesc(eExecutionProcedure.hibernate_mode, "hibernate_mode"))));
                tmpExecutionProceduresItems.Add(new KeyValueAssignment(eExecutionProcedure.lock_workstation.ToString(), LanguageStrings.GetText(EnumUtils.GetEnumValueDesc(eExecutionProcedure.lock_workstation, "lock_workstation"))));
                tmpExecutionProceduresItems.Add(new KeyValueAssignment(eExecutionProcedure.system_logout.ToString(), LanguageStrings.GetText(EnumUtils.GetEnumValueDesc(eExecutionProcedure.system_logout, "system_logout"))));
                tmpExecutionProceduresItems.Add(new KeyValueAssignment(eExecutionProcedure.winkstart_about.ToString(), LanguageStrings.GetText(EnumUtils.GetEnumValueDesc(eExecutionProcedure.winkstart_about, "winkstart_about"))));
                tmpExecutionProceduresItems.Add(new KeyValueAssignment(eExecutionProcedure.winkstart_close.ToString(), LanguageStrings.GetText(EnumUtils.GetEnumValueDesc(eExecutionProcedure.winkstart_close, "winkstart_close"))));
                tmpExecutionProceduresItems.Add(new KeyValueAssignment(eExecutionProcedure.winkstart_settings.ToString(), LanguageStrings.GetText(EnumUtils.GetEnumValueDesc(eExecutionProcedure.winkstart_settings, "winkstart_settings"))));

                ExecutionProcedureItems = tmpExecutionProceduresItems;
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Unable to Init Action Types!", Ex, EMessageTypes.Error);
            }
        }


        void InitImageItems()
        {
            try
            {
                List<ImageKeyValueAssignment> tmpImageKeyList = new List<ImageKeyValueAssignment>();
                tmpImageKeyList.Add(new ImageKeyValueAssignment(null, "", "Please select a Image from List"));

                string[] tmpImageFiles =  System.IO.Directory.GetFiles(cUserSettingsReader.ImagesPath);
                if (tmpImageFiles != null && tmpImageFiles.Count() > 0)
                {
                    string sFileExt = "";
                    string sImageName = "";
                    BitmapImage CustomIMage = null;

                    foreach (string ImageFile in tmpImageFiles)
                    {
                        sFileExt = System.IO.Path.GetExtension(ImageFile);
                        if (sFileExt.ToUpper() == ".PNG" | sFileExt.ToUpper() == ".JPG" | sFileExt.ToUpper() == ".BMP")
                        {
                            CustomIMage = null;
                            CustomIMage = new BitmapImage(new Uri(ImageFile));

                            sImageName = System.IO.Path.GetFileName(ImageFile);

                            tmpImageKeyList.Add(new ImageKeyValueAssignment(CustomIMage, ImageFile, sImageName));
                        }
                    }
                }
                AvailableCustomImages = tmpImageKeyList;
            }
            catch (Exception Ex)
            {
                throw new Exception("Error while reading Image-File-List on Disk to use in CustomItem!", Ex);
            }
        }

        ImageKeyValueAssignment FindImageKey(string Key)
        {
            try
            {
                var ListResult = from KeyValue in _AvailableImages where KeyValue.Key.ToUpper() == Key.ToUpper() select KeyValue;
                if (ListResult != null && ListResult.Count() > 0)
                    return ListResult.First();
                else
                    return null;
            }
            catch (Exception Ex)
            {
                throw new Exception("Error on selecting Image-Key!", Ex);
            }
        }

        #endregion
    }
}
